package com.mty.springboot.controller;

import com.mty.springboot.model.*;
import com.mty.springboot.service.*;
import com.mty.springboot.util.SendmailUtil;
import com.mty.springboot.util.VerifyCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 登录控制器
 */
@Controller
public class LoginController {

	// 依赖注入
	@Autowired
	private AdminService adminService;


	@Autowired
	private UserService userService;

	@Autowired
	private NoticeService noticeService;

	@Autowired
	private LossService lossService;

	@Autowired
	private ObtainService obtainService;

	@Autowired
	private ScoreService scoreService;
    /**
     * 去登录
     */
    @RequestMapping(value = "/gologin")
    public String gologin(HttpSession session) {
        return "login";
    }

	/**
	 * 登录
	 * 将提交数据(username,password)写入Admin对象
	 */
	@RequestMapping(value = "/login")
	public String login(Admin admin, Model model, HttpSession session, HttpServletRequest request) {
		if(admin.getType()==null || admin.getType().length()<1){
			model.addAttribute("msg", "请选择人员类型！");
			return "login";
		}
		Map mp = new HashMap();
		if(admin.getType().equals("01")){
			if(admin.getUsername()==null || admin.getUsername().length()<=0 ){
				model.addAttribute("msg", "请输入登录名！");
				return "login";
			}
			if(admin.getPassword()==null || admin.getPassword().length()<1){
				model.addAttribute("msg", "请输入密码！");
				return "login";
			}
			mp.put("username",admin.getUsername());
			mp.put("password",admin.getPassword());
			List<Admin> ad = adminService.queryFilter(mp);
			if(ad!=null && ad.size()==1){
				String user_type = ad.get(0).getType();
				if(!user_type.equals(admin.getType())){
					model.addAttribute("msg", "人员类型错误，请重新登录！");
					return "login";
				}
				session.setAttribute("ad", ad.get(0));
				session.setAttribute("type", "01");
				return "homepage1";
			}else{
				model.addAttribute("msg", "账号或密码输入错误，请重新登录！");
				return "login";
			}
		}else if(admin.getType().equals("02")){
			if(admin.getUsername()==null || admin.getUsername().length()<=0 ){
				model.addAttribute("msg", "请输入登录名！");
				return "login";
			}
			if(admin.getPassword()==null || admin.getPassword().length()<1){
				model.addAttribute("msg", "请输入密码！");
				return "login";
			}
			mp.put("username",admin.getUsername());
			mp.put("password",admin.getPassword());
			List<Admin> ad = adminService.queryFilter(mp);
			if(ad!=null && ad.size()==1){
				String user_type = ad.get(0).getType();
				String status = ad.get(0).getStatus();
				if(!user_type.equals(admin.getType())){
					model.addAttribute("msg", "人员类型错误，请重新登录！");
					return "login";
				}
				if(status.equals("00")){
					model.addAttribute("msg", "注册信息未审核，请等待系统管理员审核！");
					return "login";
				}
				session.setAttribute("ad", ad.get(0));
				session.setAttribute("type", "02");
				return "homepage2";
			}else{
				model.addAttribute("msg", "请确定账户信息是否正确！");
				return "login";
			}
		}else if(admin.getType().equals("03")){
			if(admin.getUsername()==null || admin.getUsername().length()<=0 ){
				model.addAttribute("msg", "请输入学号！");
				return "login";
			}
			if(admin.getPassword()==null || admin.getPassword().length()<1){
				model.addAttribute("msg", "请输入密码！");
				return "login";
			}
			mp.put("idcard",admin.getUsername());
			mp.put("password",admin.getPassword());
			List<User> ad = userService.queryFilter(mp);
			if(ad!=null && ad.size()==1) {
				String status = ad.get(0).getStatus();
				if(status.equals("00")){
					model.addAttribute("msg", "注册信息未审核，请等待管理员审核！");
					return "login";
				}
				if(status.equals("02")){
					model.addAttribute("msg", "注册信息审核拒绝，请联系管理员！");
					return "login";
				}
				if(status.equals("03")){
					model.addAttribute("msg", "账户已冻结，需要解冻请前往失物招领工作中心联系管理员！");
					return "login";
				}
				session.setAttribute("ad", ad.get(0));
				session.setAttribute("type", "03");
				return "homepage3";
			}else{
				model.addAttribute("msg", "请确定账户信息是否正确！");
				return "login";
			}
		}else{
			model.addAttribute("msg", "请确定用户类型是否正确！");
			return "login";
		}
	}

	/**
	 * 查询个人信息
	 */
	@RequestMapping(value = "/info")
	public String info(Admin admin, Model model, HttpServletRequest request) {
		HttpSession session = request.getSession();
		if(session.getAttribute("ad") == null){
			session.setAttribute("msg", "对不起，请登录！");
			return "login";
		}
		return "queryInfo";
	}

	/**
	 * 进入修改
	 */
	@RequestMapping(value = "/updateInfo")
	public String updateInfo(Admin admin, Model model, HttpServletRequest request) {
		HttpSession session = request.getSession();
		if(session.getAttribute("ad") == null){
			session.setAttribute("msg", "对不起，请登录！");
			return "login";
		}
		return "updateInfo";
	}


	/**
	 * 管理员修改信息
	 */
	@RequestMapping( value = "/updateInfoAdmin", method = RequestMethod.POST)
	@ResponseBody
	public String updateInfoAdmin(Admin admin,Model model,HttpServletRequest request,HttpSession session1) {
		HttpSession session = request.getSession();
		if(session.getAttribute("ad") == null){
			session.setAttribute("msg", "对不起，请登录！");
			return "202";
		}
		if(admin.getUsername().length()<1){
			return "203";
		}
		if(!isPhone(admin.getPhone())){
			return "205";
		}
		if(admin.getPassword().length()<1){
			return "204";
		}
		Admin admin1 = (Admin) session.getAttribute("ad");
		if(!admin1.getPassword().equals(admin.getPassword())){
			return "201";
		}
		if(!"".equals(admin.getPasswords())){
			admin.setPassword(admin.getPasswords());
		}
		int a = adminService.updateAdmin(admin);
		return "200";
	}


	/**
	 * 查询个人信息
	 */
	@RequestMapping(value = "/uinfo")
	public String uinfo(User user, Model model, HttpServletRequest request) {
		HttpSession session = request.getSession();
		if(session.getAttribute("ad") == null){
			session.setAttribute("msg", "对不起，请登录！");
			return "login";
		}
		User u = (User) session.getAttribute("ad");
		User user1 = userService.findUserById(u.getId());
		model.addAttribute("user",user1);
		return "uqueryInfo";
	}

	/**
	 * 用户进入修改
	 */
	@RequestMapping(value = "/uupdateInfo")
	public String uupdateInfo(Admin admin, Model model, HttpServletRequest request) {
		HttpSession session = request.getSession();
		if(session.getAttribute("ad") == null){
			session.setAttribute("msg", "对不起，请登录！");
			return "login";
		}
		return "uupdateInfo";
	}

	/**
	 * 用户修改信息
	 */
	@RequestMapping( value = "/updateInfoUser", method = RequestMethod.POST)
	@ResponseBody
	public String updateInfoUser(User user, Model model, HttpServletRequest request, HttpSession session1) {
		HttpSession session = request.getSession();
		if(session.getAttribute("ad") == null){
			session.setAttribute("msg", "对不起，请登录！");
			return "202";
		}
		User user1 = (User) session.getAttribute("ad");
		if(!isPhone(user.getPhone())){
			return "205";
		}
		if(user.getPassword()==null || user.getPassword().length()<1){
			return "204";
		}
		if(!user1.getPassword().equals(user.getPassword())){
			return "201";
		}
		if(!"".equals(user.getPasswords())){
			user.setPassword(user.getPasswords());
		}
		int a = userService.updateUser(user);
		return "200";
	}


	/**
	 * 前往注册
	 */
	@RequestMapping(value = "/register")
	public String register(HttpSession session) {
		session.invalidate();
		return "register";
	}
	/**
	 * 注册
	 */
	@RequestMapping(value = "/registerSub")
	public String registerSub( Admin admin, Model model, HttpSession session, HttpServletRequest request) {
		if(admin.getType().equals("02")){
			if(admin.getUsername()==null || admin.getUsername().equals("")){
				model.addAttribute("msg", "请输入用户名！");
				return "register";
			}
			if(admin.getPassword()==null || admin.getPassword().equals("")){
				model.addAttribute("msg", "请输入密码！");
				return "register";
			}
			if(admin.getPasswords()==null || admin.getPasswords().equals("")){
				model.addAttribute("msg", "请输入重复密码！");
				return "register";
			}
			if(!admin.getPasswords().equals(admin.getPassword())){
				model.addAttribute("msg", "两次输入密码不一致！");
				return "register";
			}
			List<Admin> all = adminService.getAll();
			for(int i = 0;i<all.size();i++){
				if(all.get(i).getUsername().equals(admin.getUsername())){
					model.addAttribute("msg", "用户名重复，请重试！");
					return "register";
				}
			}
			admin.setType("02");
			admin.setStatus("00");
			adminService.addAdmin(admin);
		}else{
			if(admin.getRealname()==null || admin.getRealname().equals("")){
				model.addAttribute("msg", "请输入姓名！");
				return "register";
			}
			if(admin.getUsername()==null || admin.getUsername().equals("")){
				model.addAttribute("msg", "请输入学号！");
				return "register";
			}
			if(admin.getPassword()==null || admin.getPassword().equals("")){
				model.addAttribute("msg", "请输入密码！");
				return "register";
			}
			if(admin.getPasswords()==null || admin.getPasswords().equals("")){
				model.addAttribute("msg", "请输入重复密码！");
				return "register";
			}
			if(!admin.getPasswords().equals(admin.getPassword())){
				model.addAttribute("msg", "两次输入密码不一致！");
				return "register";
			}
			List<User> all = userService.getAll();
			for(int i = 0;i<all.size();i++){
				if(all.get(i).getIdcard().equals(admin.getUsername())){
					model.addAttribute("msg", "学号重复，请重试！");
					return "register";
				}
			}
			User user = new User();
			user.setIdcard(admin.getUsername());
			user.setPassword(admin.getPassword());
			user.setRealname(admin.getRealname());
			user.setStatus("00");
			userService.addUser(user);
		}
		model.addAttribute("msg", "注册成功！等待管理员审核完可进行登录");
		return "login";
	}


	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/loginOut")
	public String loginOut(HttpSession session) {
		session.invalidate();
		return "login";
	}

	/**
	 * 判断手机号格式
	 */
	public boolean isPhone(String string) {
		if (string == null)
			return false;
		String regEx1 = "^1[3|4|5|7|8][0-9]{9}$";
		Pattern p;
		Matcher m;
		p = Pattern.compile(regEx1);
		m = p.matcher(string);
		if (m.matches())
			return true;
		else
			return false;
	}


	/**
	 * 欢迎页面
	 */
	@RequestMapping(value = "/welcome")
	public String welcome(Model model, HttpServletRequest request) {
		Map mp1 = new HashMap();
		mp1.put("sw","01");
		List<Notice> sw_notices = noticeService.queryFilter(mp1);
		model.addAttribute("sw_notices", sw_notices);
		Map mp2 = new HashMap();
		mp2.put("sw","01");
		List<Loss> losses = lossService.queryFilter(mp2);
		model.addAttribute("losses", losses);
		Map mp3 = new HashMap();
		mp3.put("sw","01");
		List<Obtain> obtains = obtainService.queryFilter(mp3);
		model.addAttribute("obtains", obtains);
		List<Score> scores = scoreService.queryBang();
		model.addAttribute("scores", scores);
		return "welcome";
	}
}
